package com.boss.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Future;

/**
 * Created by xuxiaobao on 2017/8/30.
 */
@Service
public class ConsumerService {
    @Autowired
    @LoadBalanced
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallback")
    public Future<String> helloService() {
        return new AsyncResult<String>() {
            @Override
            public String invoke() {
            return restTemplate.getForObject("http://PAY-SERVICE/hello",String.class);
            }
        };
    }

    public String helloFallback(Throwable e) {
        System.out.println("exception is :"+ e.getMessage());
        return "error";
    }
}
